import mongoose, { Document, Schema } from 'mongoose';
import { IIncome, IncomeType } from '@/types';

export interface IIncomeDocument extends Omit<IIncome, '_id'>, Document {}

const incomeSchema = new Schema<IIncomeDocument>(
  {
    userId: {
      type: String,
      required: [true, '用户ID是必需的'],
    },
    vehicleId: {
      type: String,
      required: [true, '车辆ID是必需的'],
    },
    type: {
      type: String,
      enum: Object.values(IncomeType),
      required: [true, '收入类型是必需的'],
    },
    amount: {
      type: Number,
      required: [true, '收入金额是必需的'],
      min: [0, '收入金额不能为负数'],
    },
    date: {
      type: Date,
      required: [true, '收入日期是必需的'],
      default: Date.now,
    },
    description: {
      type: String,
      trim: true,
      maxlength: [500, '描述不能超过500个字符'],
    },
  },
  {
    timestamps: true,
  }
);

// 索引
incomeSchema.index({ userId: 1 });
incomeSchema.index({ vehicleId: 1 });
incomeSchema.index({ type: 1 });
incomeSchema.index({ date: 1 });
incomeSchema.index({ userId: 1, vehicleId: 1 });
incomeSchema.index({ userId: 1, date: 1 });

export const Income = mongoose.model<IIncomeDocument>('Income', incomeSchema);
